use std::cmp::Ordering;
use std::collections::HashMap;

// 条目类型必须实现 std::cmp::Ord，这样条目之间才能相互比较
pub fn max_min_test() {
    let numbers = [-2, 0, 1, 0, -2, -5];
    println!("{:?}", numbers.iter().max());
    println!("{:?}", numbers.iter().min());
}

pub fn maxby_minby_test() {
    fn cmp(lhs: &f64, rhs: &f64) -> Ordering {
        lhs.partial_cmp(rhs).unwrap()
    }

    let numbers = [1.0, 4.0, 3.0];
    println!("{:?}", numbers.iter().cloned().max_by(cmp));
    println!("{:?}", numbers.iter().copied().min_by(cmp));
}

// 获取人口最多的城市和获取人口最少的城市
pub fn maxbykey_minbykey_test() {
    let mut populations = HashMap::new();
    populations.insert("Portland", 583_776);
    populations.insert("Fossil", 449);
    populations.insert("Greenhorn", 2);
    populations.insert("Boring", 7_762);
    populations.insert("The Dalles", 15_340);
    println!("{:?}", populations.iter().max_by_key(|&(_name, pop)| pop));
    println!("{:?}", populations.iter().min_by_key(|&(_name, pop)| pop));
}
